﻿/*
 * Cratos Desktop
 * Cratos Proxy class
 * 
 * Copyright (c) 2011 Draco
 * 
 * This file is part of Cratos.
 * 
 * Cratos is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Cratos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cratos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *       .==.        .==.
 *      //'^\\      //^'\\
 *     // ^ ^\(\__/)/^ ^^\\
 *    //^ ^^ ^/6  6\ ^^ ^ \\
 *   //^ ^^ ^/( .. )\^ ^ ^ \\
 *  // ^^ ^/\| V""V|/\^ ^ ^ \\
 * //^^/\/ /  '~~'  \ \/\^ ^ \\
 *  ------------------------------------
 *  HERE BE DRAGONS
 *  
 *  Johan Dercksen - johan@draco.co.za
 */
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Cratos.Tools.General;
using Cratos.Tools.DataObject;
using Cratos.Desktop.CratosWcfService;

namespace Cratos.Desktop
{
	/// <summary>
	/// Cratos Server Proxy class
	/// </summary>
	public class CratosProxy : CratosProxyBase, ICratosEntityAccess
	{
		#region Class Constant declarations
		#endregion

		#region Class Event declarations
		#endregion

		#region Class Variable declarations
		#endregion

		#region Class Constructors & Destructors
		/// <summary>
		/// Class Constructor
		/// </summary>
		public CratosProxy() : base() { }
		#endregion

		#region Class Properties
		#endregion

		#region Class Methods
		/// <summary>
		/// Entity Perform Action
		/// </summary>
		/// <returns>A Boolean indicating success of the method</returns>
		public bool PerformAction(ref IDOCratos cratosDO)
		{
			// Declare the required variables
			bool							returnValue = false;
			CratosWcfContract	ctCratos    = null;

			try
			{
				// Clear any pending error(s)
				this.Errors.Clear();

				// Create and Setup the WCF Contract
				ctCratos                = new CratosWcfContract();
				ctCratos.IsCollection   = false;
				ctCratos.IsCompressed   = false;
				ctCratos.DODataType		  = cratosDO.GetType().AssemblyQualifiedName;

				// Serialize the Data Object
				ctCratos.DOStream = cratosDO.CreateSerializeStream();

				// Send the request to the server
				ctCratos = CWcfService.PerformAction(ctCratos);

				// Deserialize the object
				cratosDO = cratosDO.CreateObjectFromStream(ctCratos.DOStream);

				// Process the errors returned (If Any)
				foreach (string currentError in ctCratos.Errors)
				{
					this.Errors.Add(new CratosError("EntityAccess", CratosLogSeverity.Exception, currentError));
				}
			}
			catch (Exception runtimeException)
			{
				// Create and setup a new error
				this.Errors.Add(new CratosError("CratosProxy.PerformAction", CratosLogSeverity.Exception,
																				"Runtime error ocurred", runtimeException));
			}
			finally
			{
				// Set the return value
				returnValue = (ctCratos.IsSuccess && (this.Errors.Count <= 0));
			}

			// return to calling method
			return(returnValue);
		}
		#endregion

		#region Class Support Methods
		#endregion
	}
}
